package server;
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Server_Chat {
    public void Chat() throws IOException {
        try {
            ServerSocket sk = new ServerSocket(3200);

            do {
                //System.out.println("Waiting for a client...");
                Socket ss = sk.accept();

                //System.out.println("Client has joined to chat!");
                //System.out.println(ss.getPort());

                InputStream is = ss.getInputStream();
                BufferedReader br;
                br = new BufferedReader(new InputStreamReader(is));

                OutputStream os = ss.getOutputStream();
                BufferedWriter bw;
                bw = new BufferedWriter(new OutputStreamWriter(os));
                
                do {
                    String receivedMessage = br.readLine();
                    // Client go vao "quit" thi cham dut
                    // viec chat vs Server
                    if (receivedMessage.equalsIgnoreCase("quit")) {
                        System.out.println("Client has left!");
                        break;
                    }
                    
                    System.out.print("Client : " + receivedMessage + "\nServer: ");
                    Scanner sn = new Scanner(System.in);
                    String sentMessage = sn.nextLine();

                    bw.write(sentMessage);
                    bw.newLine();
                    bw.flush();
                    
                } while (true);
                bw.close();
                br.close();
                sk.close();
            } while (true);

        } catch (IOException e) { }
    }
}